package com.mzpai.web.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

@Component
public class StorageUploadUtil {
	
	private static Logger logger = LoggerFactory.getLogger(StorageUploadUtil.class);
	private static String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKey = "LTAI4GDLQ2Na9U4jPkj6GFMz";
    private static String secretKey = "8GBVfKRSvEczKjiaTzgridYy2LxFoy";
    private static String bucketName = "mzpai";
    private static OSSClient ossClient =null;
    static{
    	 ossClient = new OSSClient(endpoint, accessKey, secretKey);
    }
   
    
    /**
     * 上传图片获取fileUrl
     * @param instream
     * @param fileName
     * @return
     */
    private static String uploadFile2OSS(InputStream instream, String fileName, String suffix) {
        String ret = "";
        PutObjectResult putResult =null;
        try {
        	fileName = fileName + "." + suffix;
            
            //上传文件
            OSSClient ossClient = new OSSClient(endpoint, accessKey, secretKey);
            
          //创建上传Object的Metadata
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(instream.available());
            objectMetadata.setContentType(contentType(suffix));
            Date expire = new Date(new Date().getTime() + 24 * 3600 * 1000);
            objectMetadata.setExpirationTime(expire);
            objectMetadata.setContentDisposition("inline;filename=" + fileName);
            logger.info("msg1=上传阿里云图片,,method=uploadFile2OSS,,bucketName={},fileName={}",bucketName,fileName);
            //上传文件
            putResult = ossClient.putObject(bucketName, fileName, instream, objectMetadata);
            if(putResult != null) {
            	ret = putResult.getETag();
            }
        } catch (Exception e) {
        	logger.error("msg1=上传图片异常,,method=uploadFile2OSS");
        	e.printStackTrace();
        } finally {
            try {
                if (instream != null) {
                    instream.close();
                }
            } catch (Exception e) {
            	logger.info("msg1=finally 上传阿里云图片,,method=uploadFile2OSS,,putResult={}",putResult);
                e.printStackTrace();
            }
        }
        logger.info("msg1=返回结果上传阿里云图片,,method=uploadFile2OSS,,ret={},,putResult={}",ret,putResult);
        return ret;
    }
    
    
    /**
	 * 上传File
	 * @param file
	 * @param fileName
	 * @param suffix
	 * @return
	 * @throws IOException
	 */
	public static String ossUploadFile(File file,String suffix) throws IOException {
		InputStream in = new FileInputStream(file);
		String name = "99" + (new Date().getTime());
		uploadFile2OSS(in, name,  suffix);
		return "https://cdn.bianwaixingqiu.com/" +  name + "." + suffix;
	}
	
	
	 /**
		 * 上传File
		 * @param file
		 * @param fileName
		 * @param suffix
		 * @return
		 * @throws IOException
		 */
		public static String ossUploadFile(InputStream in,String suffix) throws IOException {
			String name = "99" + (new Date().getTime());
			uploadFile2OSS(in, name,  suffix);
			return "https://cdn.bianwaixingqiu.com/" +  name + "." + suffix;
		}
		
		
	
	/**
     * @param fileType
     * @return String
     * @MethodName: contentType
     * @Description: 获取文件类型
     */
    private static String contentType(String fileType) {
        fileType = fileType.toLowerCase();
        String contentType = "";
        switch (fileType) {
            case "bmp":
                contentType = "image/bmp";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "png": 
            	contentType = "image/png";
                break;
            case "jpeg":
            	contentType = "image/jpeg";
                break;
            case "jpg":
                contentType = "image/jpeg";
                break;
            case "html":
                contentType = "text/html";
                break;
            case "txt":
                contentType = "text/plain";
                break;
            case "vsd":
                contentType = "application/vnd.visio";
                break;
            case "pptx":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case "docx":
                contentType = "application/msword";
                break;
            case "xml":
                contentType = "text/xml";
                break;
            case "mp4":
                contentType = "video/mp4";
                break;
            case "js":
                contentType = "application/javascript";
                break;
            case "css":
                contentType = "text/css";
                break;
            default:
                contentType = "application/octet-stream";
                break;
        }
        return contentType;
    }
    
    public static void main(String[] args) throws IOException {
		File file = new File("D://test//test3.jpg");
		System.out.println(file.exists());
		String usr=ossUploadFile(file, "jpg");
		System.out.println(usr);
	}
    
}
